package com.app.couponsharelife.ui.dialog
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.*
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.app.couponsharelife.R
import com.app.couponsharelife.ui.listener.OnClickListener
import kotlinx.android.synthetic.main.dialog_refund_cause.*
import kotlinx.android.synthetic.main.refund_cause_rvitm.view.*

/**
 *Created by xct on 2019/3/12
 *Description:
 */
class RefundCauseDialog(context: Context, themeResId: Int, private val listener: OnClickListener,private val dataList:List<String>):Dialog(context,themeResId) {
    private var index=0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_refund_cause)
        initView()
    }
    private fun initView(){
        refundCause_tvQX.setOnClickListener {
            dismiss()
        }
        refundCause_tvQR.setOnClickListener {
            listener.click(0,dataList[index])
            dismiss()
        }
        refundCause_rv.layoutManager=LinearLayoutManager(context)
        refundCause_rv.adapter=MyAdapter()
        window!!.setWindowAnimations(R.style.scale_in_alpha_out)
        window!!.setGravity(Gravity.BOTTOM)
        setCanceledOnTouchOutside(true)
        //设置dialog没有边距
        window!!.decorView.setPadding(0, 0, 0, 0)
        window!!.setWindowAnimations(R.style.window_BottomtoTop_animStyle)
        val lp = window!!.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = lp
    }
    inner class MyAdapter:RecyclerView.Adapter<RecyclerView.ViewHolder>(){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            val view=LayoutInflater.from(parent.context).inflate(R.layout.refund_cause_rvitm,parent,false)
            return MyHolder(view)
        }

        override fun getItemCount(): Int {
            return dataList.size
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            if (dataList.isNotEmpty()){
                if (holder is MyHolder){
                    holder.tvContent?.text=dataList[position]
                    if (index==position){
                        holder.ivCheck!!.setImageResource(R.drawable.imag_duihao_red)
                    }else{
                        holder.ivCheck!!.setImageResource(R.drawable.ic_act_good_share_no_select)
                    }
                    holder.itemView.setOnClickListener {
                        index=position
                        notifyDataSetChanged()
                    }
                }
            }
        }

    }
    inner class MyHolder(itemview:View):RecyclerView.ViewHolder(itemview){
        var tvContent:TextView?=null
        var ivCheck:ImageView?=null
        init {
            tvContent=itemview.tvContent
            ivCheck=itemview.ivCheck
        }
    }
}